﻿using DbControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using Models;

namespace CentralMonitoring
{
    /// <summary>
    /// 设备信息管理窗体
    /// </summary>
    public partial class AlarmConfig : Form
    {
        public AlarmConfig()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            comboBoxDevice.DisplayMember = "DeviceName";
            comboBoxDevice.ValueMember = "DeviceId";
            comboBoxDevice.DataSource = OprSql.GetDeviceAll();            
            if (comboBoxDevice.Items.Count > 0)
            {
                if(!string.IsNullOrEmpty(session.m_currentDeviceId))
                {
                    Device dv=OprSql.GetDeviceByDeviceId(session.m_currentDeviceId);
                    if(dv!=null)
                        comboBoxDevice.Text = dv.DeviceName;
                    else
                        comboBoxDevice.SelectedIndex = 0;
                }
                else
                    comboBoxDevice.SelectedIndex = 0;
            }
            RefreshGrid("");
        }
        private void ReInitCombox()
        {
            comboBoxDevice.Items.Clear();
            comboBoxDevice.DisplayMember = "DeviceName";
            comboBoxDevice.ValueMember = "DeviceId";
            comboBoxDevice.DataSource = OprSql.GetDeviceAll();
            
            if (comboBoxDevice.Items.Count > 0)
            {
                if (!string.IsNullOrEmpty(session.m_currentDeviceId))
                {
                    Device dv = OprSql.GetDeviceByDeviceId(session.m_currentDeviceId);
                    comboBoxDevice.Text = dv.DeviceName;
                }
                else
                    comboBoxDevice.SelectedIndex = 0;
            }
            RefreshGrid("");
        }
        private void DevicesConfig_Load(object sender, EventArgs e)
        {
            this.StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            InitCombox();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            DeviceEdit dlg = new  DeviceEdit();
            dlg.m_type =OprType.Add;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                RefreshGrid("");
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)//dataGridView1.Rows.Count
            {
                int nID = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;//(int)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Id"].Value;
                DeviceEdit dlg = new DeviceEdit();
                dlg.m_type =OprType.Modify;
                dlg.m_Id = nID;
                dlg.m_beforName = dataGridView1.SelectedRows[0].Cells["DeviceName"].Value.ToString();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    RefreshGrid("");
                }
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                try
                {
                    if (MessageBox.Show("确定要删除该条数据吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                        return;
                    //int currenID = dataGridView1.SelectedRows.Count;                   
                    //int nId = (int)dataGridView1.Rows[dataGridView1.SelectedRows[currenID].Index].Cells["id"].Value;
                    int nId = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                    if(OprSql.DeviceDel(nId)>0)
                        InitCombox();
                    else
                        MessageBox.Show("删除失败！");
                }
                catch (Exception ex)
                {                   
                    MessageBox.Show("删除失败！");
                    lLogWrite.WriteLog(ex.ToString());                  
                }
            }
        }

        private void RefreshGrid(string name)
        {          
            DataTable dt = OprSql.GetDeviceByName(name);
            if (dt != null)
            {
                this.dataGridView1.DataSource = dt;
                dataGridView1.Columns["Id"].Visible = false;
                dataGridView1.Columns["DeviceName"].HeaderText = "设备名称";
                dataGridView1.Columns["DeviceId"].HeaderText = "识别码";
                dataGridView1.Columns["DeviceSite"].HeaderText = "安装位置";
                dataGridView1.Columns["DeviceOil"].HeaderText = "加注油品";
                dataGridView1.Columns["topic"].HeaderText = "数据主题";
                dataGridView1.Columns["XishuA"].HeaderText = "粘度系数A";
                dataGridView1.Columns["XishuB"].HeaderText = "粘度系数B";
                dataGridView1.Columns["Flag"].HeaderText = "状态";
                dgSetView.SetFormatDataGridView(dataGridView1);
            }

        }

        private void buttonParaSet_Click(object sender, EventArgs e)
        {
            DeviceByOilParaSet dlg = new DeviceByOilParaSet();
            dlg.ShowDialog();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            //if(comboBoxDevice.Items.Count>0)
            if(!string.IsNullOrEmpty(comboBoxDevice.Text))
                RefreshGrid(comboBoxDevice.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            ServerEdit dlg=new ServerEdit();
            dlg.ShowDialog();
        }
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView))
            {
                return;
            }
            DataGridView dgv = (DataGridView)sender;
            object originalValue = e.Value;
            int dValue = 0;
            if (dgv.Columns[e.ColumnIndex].DataPropertyName == "Flag")
            {
                if (e.Value != null && int.TryParse(e.Value.ToString(), out dValue))
                    e.Value = dValue > 0 ? "启动" : "禁用";
            }
        }
    }
}
